module Utils
  module View
    def user_group_restrictions(objeck)
      groups = UserGroup.find(:all)
      return "" unless groups.length > 0

      rvalue = <<-START_HTML
        <tr>
          <td>&nbsp;</td>
          <td>
            <strong>Select the user groups that will have access</strong><br/>
            &nbsp;&nbsp;&nbsp;<strong>** Leave unchecked for public access.</strong><br/>
            &nbsp;&nbsp;&nbsp;Listed as <strong>Role:</strong> <i>Group</i><br/>
          </td>
        </tr>
        <tr>
          <td class="list_label">User Groups:</td>
          <td class="list_data">
            <ul id="user_groups" class="checklist">
      START_HTML
      i = 0

      groups.each do |grp|
        bg = row_class(i)
        i += 1
        rvalue << checklist_li("<strong>#{grp.role.name}:</strong> <i>#{grp.name}</i>","grp",grp.id,bg,objeck.has_user_group_id?(grp.id))
      end

      rvalue << <<-END_HTML
            </ul>
          </td>
        </tr>
      END_HTML
      rvalue
    end

    def checklist_li(label,name,id,cls,checked)
      ref = name + "_" + id.to_s
      ckd = "checked" if checked
      return <<-LI
        <li class="#{cls}">
          <label for="#{ref}">
            <input id="#{ref}" name="#{ref}" type="checkbox" #{ckd}/>&nbsp;&nbsp;#{label}
          </label>
        </li>
      LI
    end

    def row_class(i)
      ( i % 2 == 0 ) ? "even" : "odd"
    end

    def pagination(pages)
      rvalue = "<span id=\"pagination\">"
      if pages.current.previous
        rvalue << link_to('Previous page', { :action => 'list', :page => pages.current.previous})
        rvalue << "&nbsp;&nbsp;|&nbsp;&nbsp;" if pages.current.next
      end


      if pages.current.next
        next_page = pages.current.next
        page_no = next_page.to_i - 1
        rvalue << %(pg. #{page_no} of #{pages.length})
        rvalue << "&nbsp;&nbsp;|&nbsp;&nbsp;"
        rvalue << link_to('Next page', { :action => 'list', :page => next_page})
      end
      rvalue << "</span>"
      rvalue
    end

    def custom_pagination(pager, list_page = "list")
      rvalue = "<span id=\"pagination\">"
      if pager.previous_page
        options = { :action => list_page, :page => pager.previous_page}.merge(pager.search_by)
        rvalue << link_to('Previous page', options) 
        rvalue << "&nbsp;&nbsp;|&nbsp;&nbsp;" 
      end

      page_no = 1
      page_no = (pager.next_page - 1) if pager.next_page
      page_no = (pager.previous_page + 1) if pager.previous_page

      rvalue << %(pg. #{page_no} of #{pager.total})

      if pager.next_page
        page_no = pager.next_page - 1
        rvalue << "&nbsp;&nbsp;|&nbsp;&nbsp;"
        options = { :action => list_page, :page => pager.next_page}.merge(pager.search_by)
        rvalue << link_to('Next page', options)
      end
      rvalue << "</span>"
      rvalue
    end

    def setup_pager(params,total_records)
      pager = Pager.new
      current_page = 1
      current_page = params[:page].to_i if params[:page]
      pager.previous_page = nil
      pager.total = 1
      if total_records > 10
        pager.total, remainder = total_records.divmod(10)
        pager.total += 1 if remainder > 0
        pager.next_page = 2
        if current_page > 1
          pager.previous_page = current_page - 1
          if current_page < pager.total
            pager.next_page = current_page + 1
          else
            pager.next_page = nil
          end
        end
        pager.search_by = get_search_by(params)
      end
      pager
    end


    def pluralize(count,singular,plural = nil)
      if count == 1
        singular
      elsif plural
        plural
      else
        Inflector.pluralize(singular)
      end
    end

    def tabs(tab_names)
      tabs = []
      i = 0
      tab_names.each do |tname|
        id = tname.downcase.gsub(/ /,"_")
        style = ""
        style = %(style="color: #ccc;") if i > 0
        tabs << %(<a href="#" id="tab_#{id}" class="tab" onclick="switch_tab('#{id}')" #{style}>#{tname}</a>)
        i += 1
      end
      rvalue = %(<div class="tabs">)
      rvalue << tabs.join("&nbsp;|&nbsp;")
      rvalue << "</div>"
    end

    private
      def get_search_by(params)
        rvalue = Hash.new
        sp = params.dup
        sp.delete(:action)
        sp.delete(:controller)
        sp.delete(:commit)
        sp.delete(:page)
        rvalue = rvalue.merge(sp) unless sp.empty?
        rvalue
      end
  end #view
end #utils
